/* 
 * 新的Select
 */
define(function(require, exports, module)
{
  var $= require('chosen');
  $(function()
  {
    initChosen() ;
  });
  
  function initChosen()
  {
    $("select").each(function()
    {
      if (!$(this).is(":hidden"))
      {
        var selectWidth = $(this).width() * 1.2;
        if (undefined != $(this).attr("multiple"))
          selectWidth = selectWidth * 2 ;
        $(this).chosen({width:selectWidth + "px",disable_search_threshold: 10});
        initChosenValue($(this));
        $(this).on('change', function(e, params) 
        {
          $(this).trigger('chosen:updated');
          initChosenValue($(this));
        });
      }
    });	  
  }
  
  function initChosenValue(selectObject)
  {
    var itemValue = "" ;
    var itemId = "" ;
    selectObject.children("option:selected").each(function()
    {
      if (itemValue != "" )
        itemValue += "," ;
      if (itemId != "" )
        itemId += "," ;

      itemValue += $(this).text() ;
      itemId += $(this).val() ;
    });

    if (undefined != selectObject.attr("inputid"))
      $("#" + selectObject.attr("inputid")).val(itemId) ;
    if (undefined != selectObject.attr("inputview"))
      $("#" + selectObject.attr("inputview")).val(itemValue) ; 
  }
  exports.initChosen = initChosen;
})